/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import biz.LoginProgess;
import biz.ReplyProgess;
import da.exceptions.PreexistingEntityException;
import da.exceptions.RollbackFailureException;
import entities.Account;
import entities.Complain;
import entities.Relation;
import entities.Reply;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author THOHCGC00177
 */
@ManagedBean
@SessionScoped
public class RelationProgess {
    @EJB
    private ReplyProgess replyProgess;

    @EJB
    private biz.ComplainProgess complainProgess;
    @EJB
    private LoginProgess loginProgess;
    @EJB
    private biz.RelationProgess relationProgess;
    private String technicalName;
    private List<Relation> lstRe;
    private Relation choseRelation;
    private String msg;
    private String temp;
    private Reply listReply;
    private Complain comp;
    private Relation detail;

    /**
     *
     * Function create a relation
     *
     * @param complain
     * @return
     */
    public String createRelation(Complain complain) {
        //get account login 
        Account user = loginProgess.getUser(technicalName);
        Date date = new Date();
        try {
            //create new relation
            Relation relation = new Relation(1);
            relation.setId(1);
            relation.setComplainId(complain);
            relation.setTechnicalId(user);
            relation.setDate(date);
            //call ejb to create
            relationProgess.createRelation(relation);
            //update stt processing when admin comfirm
            if (complain.getProcessing().equals("No")) {
                complain.setProcessing("Yes");
                complainProgess.updateProcessing(complain);
            }
            detail = relation;
            msg = "Send to technical success!!";
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(ComplainProgess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            Logger.getLogger(ComplainProgess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ComplainProgess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "admin_relation_detail.xhtml";
    }

    /**
     *
     * Function show relation detail
     *
     * @param temp
     * @return
     */
    public String showRelationDetail(Relation relation) {
        if (relation.getComplainId().getStatus().equals("Complete")) {
            comp = complainProgess.getComplainById(relation.getComplainId().getId());
            listReply = replyProgess.getReplyById(relation.getComplainId().getId());
            return "reply_detail.xhtml";
        } else {
            //get relation 
            choseRelation = relationProgess.getRelationById(relation.getId());
            //update stt processing technician read complain
            if (relation.getComplainId().getStatus().equals("New")) {
                //update status of complain 
                relation.getComplainId().setStatus("Read");
                complainProgess.updateStatus(relation.getComplainId());
            }
            return "technician_reply.xhtml";
        }

    }

    public Relation getDetail() {
        return detail;
    }

    public void setDetail(Relation detail) {
        this.detail = detail;
    }

    public Relation getChoseRelation() {
        return choseRelation;
    }

    public void doCreateListRelation(int id) {
        lstRe = relationProgess.getListRelationById(id);
    }

    public List<Relation> getLstRe() {
        return lstRe;
    }

    public String getTechnicalName() {
        return technicalName;
    }

    public void setTechnicalName(String technicalName) {
        this.technicalName = technicalName;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public String getTemp() {
        return temp;
    }

    public Reply getListReply() {
        return listReply;
    }

    public void setListReply(Reply listReply) {
        this.listReply = listReply;
    }

  

    public Complain getComp() {
        return comp;
    }

    public void setComp(Complain comp) {
        this.comp = comp;
    }
    
}
